package cn.org.eshow.webapp.action;

import cn.org.eshow.bean.query.AdditionalOrderQuery;
import cn.org.eshow.common.page.Page;
import cn.org.eshow.model.AdditionalOrder;
import cn.org.eshow.model.ProductOrder;
import cn.org.eshow.service.AdditionalOrderManager;
import cn.org.eshow.service.ProductOrderManager;
import cn.org.eshow.util.PageUtil;
import cn.org.eshow.webapp.util.RenderUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Results({@Result(name = "input", location = "add"),
        @Result(name = "list", type = "redirect", location = ""),
        @Result(name = "success", type = "redirect", location = "view/${id}"),
        @Result(name = "redirect", type = "redirect", location = "${redirect}")})
@AllowedMethods({"list", "search",  "save"})
public class AdditionalOrderAction extends BaseAction {

    private static final long serialVersionUID = 1L;

    @Autowired
    private AdditionalOrderManager additionalOrderManager;
    @Autowired
    private ProductOrderManager productOrderManager;


    private List<AdditionalOrder> additionalOrders=new ArrayList<AdditionalOrder>();
    private AdditionalOrder additionalOrder=new AdditionalOrder();
    private AdditionalOrderQuery query = new AdditionalOrderQuery();

    /**
     * @return
     */
    public String list() {
        additionalOrders = additionalOrderManager.list(query);
        return LIST;
    }

    /**
     * @return
     */
    public String search() {
        Page<AdditionalOrder> page = additionalOrderManager.search(query);
        additionalOrders = page.getDataList();
        saveRequest("page", PageUtil.conversion(page));
        saveRequest("query", query);
        return LIST;
    }


    /**
     * @return
     */
    public String view() {
        if (id != null) {
            additionalOrder = additionalOrderManager.get(id);
            additionalOrder = additionalOrderManager.save(additionalOrder);
        } else {
            additionalOrder = new AdditionalOrder();
        }
        return REDIRECT;
    }



    /**
     * @return
     * @throws Exception
     */
    public void save() throws Exception {
        if(StringUtils.isEmpty(additionalOrder.getName()) || additionalOrder.getPrice()==null || additionalOrder.getProductOrder()==null || additionalOrder.getProductOrder().getId()==null){
            RenderUtil.failure("缺少参数");
            return;
        }
        ProductOrder productOrder=productOrderManager.get( additionalOrder.getProductOrder().getId());
        additionalOrder = additionalOrderManager.save(productOrder,additionalOrder);
        saveMessage("添加成功");
        RenderUtil.success("提交成功");
        return;
    }

    public List<AdditionalOrder> getAdditionalOrders() {
        return additionalOrders;
    }

    public void setAdditionalOrders(List<AdditionalOrder> additionalOrders) {
        this.additionalOrders = additionalOrders;
    }

    public AdditionalOrder getAdditionalOrder() {
        return additionalOrder;
    }

    public void setAdditionalOrder(AdditionalOrder additionalOrder) {
        this.additionalOrder = additionalOrder;
    }

    public AdditionalOrderQuery getQuery() {
        return query;
    }

    public void setQuery(AdditionalOrderQuery query) {
        this.query = query;
    }


}